package com.alibaba.citrus.cr.platform.enhance.facade.service.scitem;

import com.alibaba.citrus.cr.common.annotation.FacadeInvoker;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crplatformenhance.model.dto.ScItemMultipleLoadListRequest;
import com.epoch.app.crplatformenhance.scitemmultiple.model.ScItemMultiple;
import com.epoch.app.crplatformenhance.scitemmultiple.service.ScItemMultipleService;
import com.epoch.app.crplatformenhance.scitemmultipleread.dto.QueryRequest;
import com.epoch.app.crplatformenhance.scitemmultipleread.service.ScItemMultipleReadService;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: 货品倍数读服务
 * @author: yuhw
 * @date: 2023年02月23日 11:24
 **/
@Component
@Primary
public class ScItemMultipleReadServiceImpl implements ScItemMultipleReadService {

    private static final Log log = Log.getLogger(ScItemMultipleReadServiceImpl.class);

    @Resource
    private ScItemMultipleService scItemMultipleService;

    @Override
    @FacadeInvoker(value = "货品倍数查询",errorCode = "OTS-01-001-01-15-062")
    public Result<List<ScItemMultiple>> query(QueryRequest queryRequest) {
        log.info("ScItemMultipleReadServiceImpl_query_request:{}", JSON.toJSONString(queryRequest));
        if (check(queryRequest)){
            log.info("ScItemMultipleReadServiceImpl_query_request_param_conflict");
            return Result.success(null);
        }
        ScItemMultipleLoadListRequest loadListRequest = build(queryRequest);
        Result<List<ScItemMultiple>> listResult = scItemMultipleService.query(loadListRequest);
        log.info("ScItemMultipleReadServiceImpl_query_result:{}",JSON.toJSONString(listResult));
        listResult.setSuccess(true);
        return listResult;
    }

    /**
     * 如果 同时传了scItemId 和scItemIdList
     * @param queryRequest
     * @return
     */
    private boolean check(QueryRequest queryRequest){
        if (StringUtils.isNotBlank(queryRequest.getScItemId()) &&
            CollectionUtils.isNotEmpty(queryRequest.getScItemIdList()) &&
            !queryRequest.getScItemIdList().contains(queryRequest.getScItemId())){
            return true;
        }
        if (StringUtils.isNotBlank(queryRequest.getScItemCode()) &&
            CollectionUtils.isNotEmpty(queryRequest.getScItemCodeList()) &&
            !queryRequest.getScItemCodeList().contains(queryRequest.getScItemCodeList())){
            return true;
        }
        return false;
    }


    private ScItemMultipleLoadListRequest build(QueryRequest queryRequest){
        ScItemMultipleLoadListRequest loadListRequest = new ScItemMultipleLoadListRequest();
        List<String> scItemIdList = Lists.newArrayList();
        //因为前端页面的查询是交集  例如传了 scItemId 就以scItemId 为主 传了scItemCode后 scItemCodes失效
        if (StringUtils.isNotBlank(queryRequest.getScItemId())){
            scItemIdList.add(queryRequest.getScItemId());
        }else {
            if (CollectionUtils.isNotEmpty(queryRequest.getScItemIdList())){
                scItemIdList.addAll(queryRequest.getScItemIdList());
            }
        }
        if (CollectionUtils.isNotEmpty(scItemIdList)){
            loadListRequest.setScItemIdList(scItemIdList);
        }
        List<String> scItemCodeList = Lists.newArrayList();
        if (StringUtils.isNotBlank(queryRequest.getScItemCode())){
            scItemCodeList.add(queryRequest.getScItemCode());
        }else {
            if (CollectionUtils.isNotEmpty(queryRequest.getScItemCodeList())){
                scItemCodeList.addAll(queryRequest.getScItemCodeList());
            }
        }
        if (CollectionUtils.isNotEmpty(scItemCodeList)){
            loadListRequest.setScItemCodeList(scItemCodeList);
        }
        if (queryRequest.getStart() != null){
            loadListRequest.setStart(queryRequest.getStart());
        }
        if (queryRequest.getLimit() != null){
            loadListRequest.setLimit(queryRequest.getLimit());
        }
        if (queryRequest.getId() != null){
            loadListRequest.setId(queryRequest.getId());
        }
        loadListRequest.setDir("gmt_modified");
        loadListRequest.setAsc(Boolean.FALSE);
        return loadListRequest;
    }
}
